<template>
    <div>
        <el-form @submit.native.prevent :rules="rules" label-width="80px" style="width:400px;" ref="fromName" :model="directorFrom">
            <el-form-item label="导演名字" prop="directorName">
                <el-input  @keydown.native.enter="addBtn"  placeholder="请输入导演名字" v-model="directorFrom.directorName"></el-input>
            </el-form-item>
            <el-form-item label="导演头像" prop="directorAvatar">
                <el-upload
                    class="avatar-uploader"
                    :action="`${$UPLOADURL}/upload`"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload">
                    <img v-if="directorFrom.directorAvatar" :src="directorFrom.directorAvatar" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addBtn">立即新增</el-button>
                <el-button>取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import httpApi from "@/http/index"
    export default {
        data() {
            return {
                directorFrom:{
                    directorName:'',
                    directorAvatar:''
                    },
                rules:{
                    directorName:[
                    { required: true, message: '内容不能为空', trigger: 'change' }
                    ],
                    directorAvatar:[
                    { required: true, message: '头像路径不能为空', trigger: 'change' }
                    ],
                }
            }
        },
        methods: {
            // 提交导演按钮方法
            addBtn(){
                this.$refs['fromName'].validate((valid)=>{
                    if(valid){
                        console.log("表单验证成功,开始发起请求");
                        // let url = 'http://localhost:3010/movie-director/add'
                        // myaxios.post(url,this.directorFrom).then((res)=>{
                        //     console.log('导演信息返回结果',res)
                        //     if(res.data.code == 200){
                        //         this.$message({message:'添加成功',type:"success"})
                        //     }else{
                        //         this.$message({message:'添加失败',type:"error"})
                        //     }
                        //     this.$refs['fromName'].resetFields()
                        // })
                        httpApi.directorApi.addDirector(this.directorFrom).then(res=>{
                            console.log('导演信息返回结果',res)
                            if(res.data.code == 200){
                                this.$message({message:'添加成功',type:"success"})
                                this.$refs['fromName'].resetFields()
                            }else{
                                this.$message({message:'添加失败',type:"error"})
                            }
                        })
                    }else{
                        console.log("表单验证失败")
                    }
                })
            },
            // 头像获取后执行
            handleAvatarSuccess(res, file) {
                console.log(res);
                this.directorFrom.directorAvatar = res.data
                // 移除错误验证消息
                this.$refs['fromName'].clearValidate('directorAvatar')
            },
            // 头像获取前做的逻辑判断
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }
        },
    }
</script>

<style lang="scss" scoped>

</style>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 130px;
    height: 130px;
    line-height: 130px;
    text-align: center;
  }
  .avatar {
    width: 130px;
    height: 130px;
    display: block;
    object-fit: cover;
  }</style>